<?php
/**
 * Created by PhpStorm.
 * User: 何婷婷
 * Date: 2016/11/8
 * Time: 14:24
 */

namespace App\Controller;

use App\Core\Controller;
use App\Core\Model;
use App\Core\View;
use App\Model\UserModel;

session_start();

/**
 * Controllers 文件夹包含负责处理用户输入和响应的控制器类
 */
class UserController extends Controller
{

    /**
     * UserController constructor.
     */

    protected $user;
    protected $id;

    public function __construct()
    {
        parent::__construct();
        $this->user = new userModel();

    }

    //调用view表单
    public function showRegister()
    {
        View::showPage('user', 'showRegister');
    }

    //调用view表单
    public function showLogin()
    {
        View::showPage('user', 'showLogin');
    }

    //调用view表单
    public function showEditUser()
    {
        View::showPage('user', 'showEditUserInfo');
    }

    //调用view表单
    public function showUserPsw()
    {
        View::showPage('user', 'showUpdatePwd');
    }

    //用户注册信息填写
    public function register()
    {
        if (empty($_POST['name']) || empty($_POST['password']) || empty($_POST['cpassword']) || empty($_POST['tel']) || empty($_POST['qq']) || empty($_POST['email']))
            View::showMsg("输入信息不能为空!<a href='index.php?con=user&act=show_register'>返回注册界面</a>");

        if ($this->user->isExistName($_POST['name'])) {
            View::showMsg("用户名已经存在！<a href='index.php?con=user&act=show_register'>返回注册界面</a>");
        }
        if ($this->user->isExistTel($_POST['tel'])) {
            View::showMsg("该电话已经存在，请重新填写！<a href='index.php?con=user&act=show_register'>返回注册界面</a>");
        }
        if ($this->user->isExistEmail($_POST['email'])) {
            View::showMsg("该邮箱已经存在，请重新填写！<a href='index.php?con=user&act=show_register'>返回注册界面</a>");
        }

        $this->user->name = $_POST['name'];
        $this->user->password = md5($_POST['password']);
        $this->user->tel = $_POST['tel'];
        $this->user->email = $_POST['email'];
        $this->user->qq = $_POST['qq'];
        $this->user->create_at = time();

        if ($this->user->save())
            View::showMsg("注册成功！<a href='index.php?con=user&act=show_login'>点击登录</a>");
        else
            View::showMsg('用户信息存储失败！');
    }


    //用户登录信息验证
    public function userLoginInfo()
    {
        $result = $this->user->check($_POST['name'], $_POST['password']);
        if ($result) {
            View::showMsg("登录成功！<br><a href='index.php?con=user&act=editUserInfo'>点击编辑用户信息</a>", $this->listUser());
            $_SESSION['name'] = $_POST['name'];
            $_SESSION['password'] = $_POST['password'];
        } else {

            View::showMsg("用户或密码填写有误，请重新填写！<a href='index.php?con=user&act=show_login'>返回</a>");
        }
        session_destroy();
    }

    //显示用户信息
    public function listUser()
    {
        return $this->user->listUserInfo($_POST['name']);
    }


    //编辑用户信息
    public function editSelf()
    {
        //$result=array();
        if (empty($_POST['name']) || empty($_POST['newName']) || empty($_POST['newPassword']) || empty($_POST['newTel']) || empty($_POST['qq']) || empty($_POST['newEmail']))
            View::showMsg("输入信息不能为空!<a href='index.php?con=user&act=editUserInfo'>返回编辑用户信息界面</a>");
        if ($this->user->isExistName($_POST['name']))
            /* $result = $this->listUser();
             $this->id=$result['id'];
            //  var_dump($this->id);*/
            $this->user->name = $_POST['newName'];
        $this->user->password = $_POST['newPassword'];
        $this->user->tel = $_POST['newTel'];
        $this->user->qq = $_POST['qq'];
        $this->user->email = $_POST['newEmail'];
        //var_dump($this->user->data);
        if ($this->user->save($_POST['name'])) {
            View::showMsg("编辑成功！，请重新登录<br><a href='index.php?con=user&act=show_login'>点击登录</a>");
        } else
            View::showMsg('用户信息存储失败！');
    }

    //修改密码
    public function updatePwd()
    {

        if (empty($_POST['name']) || empty($_POST['tel']) || empty($_POST['email']) || empty($_POST['newPassword'])) {

            View::showMsg("输入信息不能为空!<a href='index.php?con=user&act=password'>返回编辑用户信息界面</a>");
        }
        if ($this->user->isExistName($_POST['name']) || $this->user->isExistTel($_POST['tel']) || $this->user->isExistEmail($_POST['email'])) {
            View::showMsg('用户输入信息有误!请重新填写<br><a href="index.php?con=user&act=password">返回</a> ');
        }
            if (!empty($_POST['newPassword'])) {
                $this->user->password = $_POST['newPassword'];
            }
                if ($this->user->save($_POST['name'])) {
                    View::showMsg('密码找回，请重新登录！<br><a href="index.php?con=user&act=show_login">点击登录</a>');
                } else
                    View::showMsg('用户信修改失败!');
    }
}